package com.ym.controller.management;

import com.github.pagehelper.PageInfo;

import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.excel.ExportExcelUtil;
import com.ym.core.utils.excel.ImportExcelUtil;

import com.ym.medical.domain.entity.HospitalDeptEntity;
import com.ym.medical.domain.entity.HospitalEntity;
import com.ym.medical.domain.param.HospitalParam;
import com.ym.medical.domain.vo.DoctorDetailsVo;
import com.ym.medical.service.IDoctorDetailSV;
import com.ym.medical.service.IHospitalDeptSV;
import com.ym.medical.service.IHospitalSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 综合管理中心
 */
@Api(tags = "医院管理")
@Controller
@RequestMapping("/management/hospital")
public class HospitalController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(HospitalController.class);

	@Autowired
	private IHospitalSV iHospitalSv;

	@Autowired
	private IHospitalDeptSV iHospitalDeptSV;

	@Autowired
	private IDoctorDetailSV iDoctorDetailSV;

	@ApiOperation("医院列表查询检索")
	@GetMapping("/getHosList")
	@ResponseBody
	public APIResponse<PageInfo<HospitalEntity>> getHospitalList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "hospitalModel", value = "医院信息", required = false)
					HospitalParam hospitalModel) throws Exception {

		PageInfo<HospitalEntity> hospital = iHospitalSv.getHospitalList(pageNo, limit, hospitalModel);
		return successData(hospital);
	}

	@Log
	@ApiOperation("医院信息导入")
	@PostMapping("/uploadHos")
	@ResponseBody
	public APIResponse<Map<String, Object>> uploadHos(
			@ApiParam(name = "excelFile", value = "医院EXCEL信息", required = true) @RequestBody MultipartFile excelFile) throws Exception {
		if (excelFile.isEmpty()) {
			return failMsg("文件不存在");
		}
		InputStream inputStream = null;
		Map<String, Object> map = new HashMap<>();
		int startLine = 2;
		//int rows =0;
		try {
			String fileName = excelFile.getOriginalFilename();
			inputStream = excelFile.getInputStream();
			List<HospitalEntity> hospitalList =
					ImportExcelUtil.readExcel
							(inputStream, fileName, startLine, HospitalEntity.class, ImportExcelUtil.getHospitalTemplate());
			if (hospitalList.isEmpty()) {
				throw new BusinessException("您的excel没有有效的数据 请添加数据后再试!");
			}
			map = iHospitalSv.uploadIntoHospital(hospitalList);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw new BusinessException(e.getMessage());
			}
			LOGGER.error(e.getMessage());
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
		}

		return successData(map);
	}

	@ApiOperation("医院信息导出")
	@GetMapping("/exportHos")
	public void exportHos(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "100000")
					int limit,
			@ApiParam(name = "hospitalModel", value = "医院信息", required = false)
					HospitalParam hospitalModel,
			HttpServletResponse response) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医院信息表";
		String[] headerString = {"编码", "医院名称", "医院等级", "所属区域", "医院介绍", "地址", "联系方式", "邮箱"};
		String[] headerField = {"hosCode", "hosName", "hosLevelName", "area", "introduction", "addrDesc", "phoneNo", "email"};
		PageInfo<HospitalEntity> hospitalPageInfo = iHospitalSv.getAllhospitalList(pageNo, limit, hospitalModel);
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, hospitalPageInfo.getList(), response, null);
	}

	@Log
	@ApiOperation("医院新增")
	@PostMapping("/addHospital")
	@ResponseBody
	public APIResponse addHospital(
			@ApiParam(name = "hospital", value = "医院信息", required = true)
			@RequestBody HospitalEntity hospital) throws Exception {
		int rows = iHospitalSv.addHospital(hospital);
		if (rows != 1) {
			return failMsg("添加失败");
		}

		return successMsg("添加成功");

	}

	@Log
	@ApiOperation("医院更改")
	@PostMapping("/updateHospital")
	@ResponseBody
	public APIResponse updateHospital(
			@ApiParam(name = "hospital", value = "医院信息", required = true)
			@RequestBody HospitalEntity hospital) throws Exception {
		int rows = iHospitalSv.updateHospital(hospital);
		if (rows != 1) {
			return failMsg("更改失败");
		}
		return successMsg("更改成功");
	}

	@Log
	@ApiOperation("医院删除")
	@PostMapping("/deleteHospital")
	@ResponseBody
	public APIResponse deleteHospital(
			@ApiParam(name = "hospIds", value = "医院ids数组", required = true) @RequestBody Long[] hospIds) throws Exception {
		int rows = iHospitalSv.deleteByIds(hospIds);
		if (rows == 0) {
			return failMsg("删除失败");
		}
		return successMsg("删除成功");
	}

	@Log
	@ApiOperation("医院code 校验")
	@GetMapping("/repeat/HosCode")
	@ResponseBody
	public APIResponse repeatHosCode(
			@ApiParam(name = "hosCode", value = "医院编码", required = true)
			@RequestParam String hosCode) {
		iHospitalSv.repeatHosCode(hosCode);
		return success();
	}


	@Log
	@ApiOperation("医院名下拉搜索")
	@GetMapping("/dropdown/search")
	@ResponseBody
	public APIResponse<List<HospitalEntity>> dropdownSearch(
			@ApiParam(name = "hosName", value = "医院名")
			@RequestParam(required = false) String hosName) {
		List<HospitalEntity> hosNames = iHospitalSv.dropdownSearch(hosName);
		return successData(hosNames);
	}

	@Log
	@ApiOperation("科室名下拉搜索")
	@GetMapping("/dropdown/search/dept")
	@ResponseBody
	public APIResponse searchDept(
			@ApiParam(name = "hosName", value = "医院名", required = false)
			@RequestParam(required = false) String hosName,
			@ApiParam(name = "deptName", value = "科室名", required = false)
			@RequestParam(required = false) String deptName) {

		List<HospitalDeptEntity> deptsList = iHospitalDeptSV.searchDept(hosName, deptName);
		return successData(deptsList);
	}

	@Log
	@ApiOperation("医生名下拉搜索")
	@GetMapping("/dropdown/search/doctName")
	@ResponseBody

	public APIResponse searchDoctName(
			@ApiParam(name = "hosName", value = "医院名")
			@RequestParam(required = false) String hosName,
			@ApiParam(name = "doctName", value = "医生名")
			@RequestParam(required = false) String doctName) {
		List<DoctorDetailsVo> doctNames = iDoctorDetailSV.searchDoctName(hosName, doctName);
		return successData(doctNames);
	}
}
